home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt0187b.arc / SAVERGN.ASM < prev    next >
Assembly Source File  |  1986-08-12  |  10KB  |  483 lines

  1. ; SaveRgn.ASM    20 July '86  h. katz
  2.  
  3. ; Abstracted from < RgnMaker.ASM >
  4.  
  5. ; This module handles the code for creating a Macintosh resource from
  6. ; the region in the Work Area which was produced by the contouring
  7. ; algorithm in < Traverse.ASM >.
  8.  
  9. ; It uses a Dialog Box to get the user-input parameters for defining
  10. ; the Resource Type, ID, and Name, reloading these DITLs from memory if a
  11. ; resource has already been written to disk this session.
  12.  
  13. ; The program uses the _Pack3 SFPutFile routine to get the name of
  14. ; the file the user wishes to add the resource to.
  15.  
  16.  
  17. INCLUDE MacTraps.D
  18.  
  19. XDEF    Save_To_File, Have_Prior_DITLs    ; defined here but
  20. XDEF    ItemHit, PutFile_Posit        ; used in < RgnMaker.ASM >
  21.  
  22. XREF    Save_Image, Restore_Image, Stop_Alert, RgnHandle, UnHiLite
  23.  
  24. SFPutFile        equ    1    ; selector for _Pack3
  25. PutFile_ID        equ    -3999
  26. Replace_DITL        equ    -3996
  27.  
  28. ioNamePtr        equ    18
  29. ioVRefNum        equ    22
  30.  
  31. StringToNum        equ    1
  32.  
  33. Res_DLOG_ID        equ    100
  34. Cancel_Button        equ    2
  35. Res_Type_DITL_Num    equ    7
  36. Res_ID_DITL_Num        equ    8
  37. Res_Name_DITL_Num    equ    9
  38.  
  39.  
  40. Save_To_File
  41.  
  42.     _InitCursor
  43.     
  44.     move    #Res_DLOG_ID, d3    ; ResID of upcoming Dialog
  45.     move.l    #'DLOG', a3
  46.     BSR    Save_Image        ; save Bits to be hidden by the DLOG
  47.  
  48.     bsr    Open_Res_DLOG
  49.     tst.b    Have_Prior_DITLs(a5)    ; did we do this before ?
  50.     BEQ.s    Get_User_Choice        ; no - first time
  51.     
  52.     BSR    Reload_Old_DITLs
  53.     
  54. Get_User_Choice
  55.  
  56.     st    Have_Prior_DITLs(a5)
  57.     
  58.     BSR    Modal_DLOG        ; get kbd-input for Resource Type,
  59.     tst    d4            ; ID, and Name
  60.     bmi.s    Save_To_File        ; input param error - try again
  61.     
  62.     pea    Res_DLOG_Storage    ; all done - dispose of Dialog
  63.     _CloseDialog            ; (and erase from screen)
  64.     
  65.     BSR    Restore_Image        ; redraw what was underneath
  66.     
  67.     cmp    #Cancel_Button, d4
  68.     beq    UnHiLite        ; User selected 'Cancel'
  69.     
  70.     BSR    Put_To_File        ; everything OK - SFPutFile
  71.     
  72.     BRA    UnHiLite
  73.  
  74. Reload_Old_DITLs    ; restore Dialog Items saved in prior session
  75.     
  76.     move    #Res_Type_DITL_Num, D4
  77.     BSR    Get_Item_Handle
  78.     move.l    ItemHandle, -(sp)
  79.     pea    Resource_Type_STR
  80.     _SetIText
  81.  
  82.     move    #Res_Id_DITL_Num, D4
  83.     BSR    Get_Item_Handle
  84.     move.l    ItemHandle, -(sp)
  85.     pea    Resource_ID_Str
  86.     _SetIText
  87.     
  88.     move    #Res_Name_DITL_Num, D4
  89.     BSR    Get_Item_Handle
  90.     move.l    ItemHandle, -(sp)
  91.     pea    Resource_Name
  92.     _SetIText
  93.  
  94.     RTS
  95.     
  96.     
  97. Get_Item_Handle
  98.  
  99.     pea    Res_DLOG_Storage    ; DLOG Ptr
  100.     move    d4, -(sp)        ; Item Number
  101.     pea    ItemType        ; not used
  102.     pea    ItemHandle        ; passed to following ROM call
  103.     pea    ItemBox            ; not used
  104.     _GetDItem
  105.     
  106.     RTS    
  107.     
  108.  
  109. Put_To_File
  110.  
  111. ; The user has successfully specified the Resource TYPE and ID.
  112. ; Append it to an existing file, or create a new one if necessary
  113.  
  114. ; The 1st piece of code replaces the message 'Replace Existing ...'
  115. ; with an 'Are You Sure ?', which makes more sense in the
  116. ; circumstances, as we're not replacing anything.
  117.  
  118.         
  119.     clr.l    -(sp)
  120.     move.l    #'DITL', -(sp)
  121.     move    #Replace_DITL, -(sp)    ; 'Replace Existing File' DITL
  122.     _GetResource
  123.     move.l    (sp)+, a0        ; get the Handle
  124.  
  125.     cmpa.l    #0, a0
  126.     BEQ.s    @SFPutFile        ; couldn't get DITL - forget it
  127.     
  128.     move.l    (a0), a0        ; get the Ptr to DITL data
  129.     move    -2(a0), d0        ; Block size ( in Block Header )
  130.     
  131. @Search_Length_Byte
  132.  
  133.     cmp.b    #$17, (a0)+        ; look for matching String Length
  134.     dBEQ    d0, @Search_Length_Byte
  135.     cmp    #-1, d0
  136.     BEQ.s    @SFPutFile        ; fell thru without finding match
  137.     
  138.     cmp.b    #'R', (a0)        ; check for 'R' of 'Replace'
  139.     BNE.s    @SFPutFile        ; forget it
  140.  
  141.     lea    'Are You Sure ?         ', a1
  142.     
  143.     move.b    (a1)+, d1        ; save Length Byte
  144.     ext.w    d1            ; get rid of what was in hi nibble
  145.     sub    #1, d1
  146.     
  147. @Replace_Str
  148.  
  149.     move.b    (a1)+, (a0)+
  150.     DBRA    d1, @Replace_Str
  151.     
  152. @SFPutFile
  153.  
  154.     move    #PutFile_ID, d3        ; ResID of upcoming Dialog
  155.     move.l    #'DLOG', a3
  156.     BSR    Save_Image        ; save Bits to be hidden by the DLOG
  157.     
  158.     move.l    PutFile_Posit, -(sp)    ; Global TopLeft of Dialog
  159.     pea    'Append Resource to File'
  160.     pea    'Resources'
  161.     move.l    #0, -(sp)
  162.     pea    SFReply
  163.     move    #SFPutFile, -(sp)
  164.     _Pack3
  165.  
  166.     BSR    Restore_Image        ; redraw what was underneath
  167.     
  168.     move.b    good, d0
  169.     tst.b    d0            ; Did user 'Cancel' out ?
  170.     beq    @Rts            ; yes
  171.         
  172.     BSR    Create_ioPB    
  173.     _GetVol                ; get Info on the Default Volume
  174.     
  175.     move    iovRefNum(a0), Default_Vol(a5)    ; save the RefNum
  176.     move    iovRefNum(a0), d0        ; to restore it later
  177.     cmp    vRefNum, d0        ; is file we want to add resource to
  178.                     ; on the Default Volume ?
  179.     beq.s    @1            ; yes
  180.     
  181.     clr.l    ioNamePtr(a0)        ; no
  182.     move    vRefNum, iovRefNum(a0)
  183.     _SetVol                ; make it the Default Volume
  184.     
  185. @1    add.l    d4, sp            ; clear the ioPB off the stack
  186.  
  187.     clr    -(sp)
  188.     _CurResFile
  189.     move    (sp)+, CurResFile(a5)    ; save the refNum for later
  190.     
  191.     clr    -(sp)
  192.     pea    fName            ; File Name that user entered
  193.     _OpenResFile            ; try to open it
  194.     move    (sp)+, d3        ; save the refNum for later
  195.  
  196.     clr    -(sp)
  197.     _ResError            ; see if we could open it
  198.     move    (sp)+, d0
  199.     tst    d0
  200.     beq.s    @2            ; we could
  201.     
  202.     pea    fName            ; we couldn't
  203.     _CreateResFile            ; so try to create it
  204.     clr    -(sp)
  205.     _ResError
  206.     move    (sp)+, d0
  207.     tst    d0
  208.     bne    @9            ; we couldn't create it
  209.     
  210.     clr    -(sp)            ; we created it
  211.     pea    fName            ; now try to open it
  212.     _OpenResFile
  213.     move    (sp)+, d3        ; save the refNum for later
  214.     clr    -(sp)
  215.     _ResError            ; check if we were able to open
  216.     move    (sp)+, d0
  217.     tst    d0
  218.     
  219.     beq.s    @2            ; hunky-dory
  220.     
  221.     move    #105, d3        ; 'Can't add to file' message
  222.     BSR    Stop_Alert
  223.     bra    @9            
  224.     
  225. @2    move    d3, -(sp)        ; we were able to open the specified file
  226.     _UseResFile            ; make it the current Resource File
  227.     
  228.     clr.l    -(sp)            ; check to see if we have a duplicate
  229.     move.l    Resource_Type, -(sp)    ; push Type (w/out Length Byte)
  230.     move.l    Resource_ID, d0        ; push low word of ID
  231.     move    d0, -(sp)
  232.     _GetResource
  233.     
  234.     move.l    (sp)+, d0        ; check the returned Handle
  235.     beq.s    @3            ; NIL Handle = OK, no Duplicate
  236.     
  237.     clr    -(sp)            ; space for INT result
  238.     move.l    d0, -(sp)        ; push handle again
  239.     _HomeResFile
  240.     move    (sp)+, d0        ; get refNum
  241.     cmp    d0, d3            ; is it in the currResFile we're using ?
  242.     bne.s    @3            ; no = OK
  243.                     
  244.     move    #104, d3        ; alertID for 'Duplicate Resource'
  245.     BSR    Stop_Alert
  246.     bra    @5            ; detach the resource
  247.     
  248. @3    move.l    RgnHandle(a5), -(sp)    ; point to the Region ( theData )
  249.     move.l    Resource_Type, -(sp)
  250.     move.l    Resource_ID, d0        ; ID saved initially as a LONG
  251.     move    d0, -(sp)        ; push low word only
  252.     pea    Resource_Name
  253.     _AddResource
  254.     
  255.     clr    -(sp)            ; check for errors
  256.     _ResError
  257.     move    (sp)+, d0        ; get the result
  258.     beq.s    @4            ; no error
  259.     
  260.     move    #106, d3        ; 'Can't Add to File' AlertID
  261.     BSR    Stop_Alert
  262.     bra    @9
  263.     
  264. @4    move.l    RgnHandle(a5), -(sp)
  265.     _WriteResource
  266.     clr    -(sp)            ; check for errors
  267.     _ResError
  268.     move    (sp)+, d0
  269.     beq.s    @5            ; no error
  270.     
  271.     move    #105, d3        ; 'Can't Add to File' AlertID
  272.     BSR    Stop_Alert
  273.     bra    @9
  274.     
  275. @5    move.l    RgnHandle(a5), -(sp)    ; just a region again
  276.     _DetachResource            ; (no longer a resource)
  277.     
  278. @9    BSR    Create_ioPB        ; restore original Default Volume
  279.     move    Default_Vol(a5), iovRefNum(a0)
  280.     _SetVol
  281.     add.l    d4, sp            ; clear ioPB off the stack
  282.     
  283.     move    CurResFile(a5), -(sp)
  284.     _UseResFile            ; restore prior Resource File
  285.     
  286. @Rts    RTS
  287.         
  288.  
  289. Create_ioPB
  290.  
  291.     move.l    (sp)+, a1
  292.     move.l    #100, d0    ; set up a temp ioPB on the stack
  293.     move.l    d0, d4        ; save for cleanup
  294.     asr    #2, d0        ; 100 Bytes -> 25 Longs (more than we really
  295.     sub    #1, d0        ; need, actually)
  296.  
  297. @Push
  298.     move.l    #0, -(sp)
  299.     dbra    d0, @Push
  300.     
  301.     move.l    sp, a0        ; sp = addr of ioParamBlock
  302.     jmp    (a1)
  303.     
  304.  
  305. Open_Res_DLOG
  306.  
  307.     clr.l    -(sp)            ; space for funct result
  308.     move    #Res_DLOG_ID, -(sp)
  309.     pea    Res_DLOG_Storage
  310.     move.l    #-1, -(sp)        ; in front of everything
  311.     _GetNewDialog
  312.     move.l    (sp)+, d0
  313.     RTS
  314.  
  315.  
  316. Modal_DLOG
  317.  
  318.     clr.l    -(sp)        ; no filterProc
  319.     pea    ItemHit
  320.     _ModalDialog
  321.     move    ItemHit, d4
  322.     
  323.     cmp    #1, d4        ; is it 'OK' ?
  324.     beq.s    @OK_Button    ; yes - save DITL EditText Items
  325.     cmp    #2, d4        ; is it 'Cancel' ?
  326.     BNE    Modal_DLOG    ; not yet - stick around
  327.     
  328.     RTS
  329.     
  330. @OK_Button
  331.  
  332.     move    #Res_Type_DITL_Num, d3
  333.     bsr    Save_Text
  334.  
  335.     lea    Resource_Type_Str, a0
  336.     move.b    (a0)+, d0        ; also save the Resource Type
  337.     lea    Resource_Type, a1    ; w/o a Length Byte for _AddResource
  338.     move.b    (a0)+, (a1)+
  339.     move.b    (a0)+, (a1)+
  340.     move.b    (a0)+, (a1)+
  341.     move.b    (a0)+, (a1)+
  342.  
  343.     move    #Res_ID_DITL_Num, d3    ; save the ID Number 
  344.     bsr    Save_Text        ; ( both as Str and INT )
  345.     move    #Res_Name_DITL_Num, d3    ; save the Resource Name
  346.     bsr    Save_Text
  347.     
  348.     lea    Resource_Type_STR, a0
  349.     move.b    (a0), d0        ; get the Length Byte
  350.     
  351.     cmp    #4, d0
  352.     BEQ.s    @Check_ID        ; length 4 = OK
  353.     
  354.     move    #103, d3        ; 'ResType must be 4 Chars'
  355.     BRA    Param_Err
  356.     
  357. @Check_ID
  358.     
  359.     move    #106, d3        ; assume '0 - 32767' error
  360.     
  361.     move.l    Resource_ID, d0        ; get the INT form of the ResID
  362.     cmp.l    #32767, d0
  363.     bls    @1            ; OK so far
  364.     
  365.     bra    Param_Err        ; whoops - larger than 32767
  366.     
  367. @1    lea    Resource_ID_Str, a0
  368.     move.b    (a0)+, d0        ; String Length
  369.     cmp.b    #0, d0            ; length = 0 ?
  370.     bne.s    @2            ; no
  371.         
  372.     BRA    Param_Err        ;  no ID entered
  373.     
  374. @2    move    #107, d3        ; assume 'Resource ID must be 0 ... 9'
  375.                     ; error
  376.     sub.b    #1, d0            ; check that each char is digit
  377.     ext.w    d0
  378.     
  379. @Loop    cmp.b    #'0', (a0)
  380.     bpl.s    @3            ; OK so far
  381.     
  382.     BRA    Param_Err
  383.     
  384. @3    cmp.b    #'9', (a0)+
  385.     dbHI    d0, @Loop
  386.     cmp    #-1, d0            ; did we exit on an error ?
  387.     bne    Param_Err        ; yes
  388.     
  389.     move    #0, d4            ; no error flag
  390.     
  391.     RTS
  392.  
  393.     
  394. Param_Err    ; ResID of Error String in d3
  395.     
  396.     move    #-1, d4            ; errFlag
  397.  
  398.     pea    Res_DLOG_Storage
  399.     _CloseDialog
  400.     
  401.     BSR    Restore_Image
  402.     
  403.     BSR    Stop_Alert
  404.     
  405.     RTS
  406.         
  407.  
  408. Save_Text
  409.  
  410.     pea    Res_DLOG_Storage
  411.     move    d3, -(sp)        ; Item Number
  412.     pea    ItemType
  413.     pea    ItemHandle
  414.     pea    ItemBox            ; get the Handle, given
  415.     _GetDItem            ; the Item Number in D3    
  416.         
  417.     move.l    ItemHandle, -(sp)
  418.     
  419.     cmp    #Res_Type_DITL_Num, d3
  420.     bne.s    @1
  421.     pea    Resource_Type_STR
  422.     _GetIText
  423.     
  424.     RTS
  425.     
  426. @1    cmp    #Res_ID_DITL_Num, d3
  427.     bne.s    @2
  428.     
  429.     pea    Resource_ID_Str        ; first save the ID as a String
  430.     _GetIText
  431.     
  432.     lea    Resource_ID_Str, a0    ; then save it as an INTEGER
  433.     move    #StringToNum, -(sp)    ; ( actually a LONG )
  434.     _Pack7
  435.     lea    Resource_ID, a0
  436.     move.l    d0, (a0)
  437.     
  438.     RTS
  439.  
  440. @2    pea    Resource_Name
  441.     _GetIText
  442.     
  443.     RTS
  444.  
  445.     
  446. ; ----------  CONSTs ( PC-relative )  --------------
  447.  
  448. Res_DLOG_Storage    dcb.b    170, 0
  449.  
  450. PutFile_Posit        dc.l    $00340064    ; TopLeft = ( 52, 100 )
  451.  
  452. SFReply
  453.    good:        dc.b    0
  454.    copy:        dc.b    0
  455.    fType:        dc.l    0
  456.    vRefNum:        dc.w    0
  457.    version:        dc.w    0
  458.    fName:        dcb.b    64, 0
  459.    
  460. ItemHit            dc    0
  461. ItemType        dc    0
  462. ItemHandle        dc.l    0
  463. ItemBox            dcb.l    2, 0
  464.  
  465. tempString        dcb.b    40, 0
  466.  
  467. Resource_Type_Str    dcb.b    10, 0    ; allow for overage
  468. Resource_ID_Str        dcb.b    10, 0    ; resID as a String
  469. Resource_Name        dcb.b    20, 0
  470.  
  471. Resource_Type        dc.l    0    ; ResType w/out Length Byte
  472. Resource_ID        dc.l    0    ; resID as a LONG ( really INT )
  473.  
  474.  
  475. ; -----------  VARS ( off A5 )  ------------------
  476.  
  477. Have_Prior_DITLs    ds.b    1
  478. Default_Vol        ds    1
  479. CurResFile        ds    1
  480.  
  481.  
  482. END